//
//  HWbuqiantakuanView.m
//  HWanGame
//
//  Created by 陈道斌 on 2021/1/14.
//  Copyright © 2021 Chendaobin. All rights reserved.
//

#import "HWbuqiantakuanView.h"




@interface HWbuqiantakuanView()


@property(strong,nonatomic) UIView *grayView;
//@property(strong,nonatomic) UIView *whiteView;
@property(strong,nonatomic) UIView *fenxiangView;
@property(strong,nonatomic) NSArray *icoArr;
@property(strong,nonatomic) NSArray *listArr;

@property(strong,nonatomic) UIImageView *fenxiangImge;
@property(strong,nonatomic)UILabel *label1;
@property(strong,nonatomic) UIButton *xiaohaoBut;

@property(strong,nonatomic) UIImageView *icoiamgeView;
@property(strong,nonatomic) UILabel *nichengLabel; // 昵称
@property(strong,nonatomic) UILabel *tianshulabel; // 天数
@property(strong,nonatomic) UILabel *buqiansihuLabel; //补签次数

@property(strong,nonatomic) UILabel *riqimiaoshulabel;




@end


@implementation HWbuqiantakuanView
- (instancetype)initWithFrame:(CGRect)frame  {
    
    if (self  = [super initWithFrame:frame]) {
        // 添加灰色背景
        [self addgrayView];
        
        [self setupUI];
        
        [self zuixingqingqiushuju];
        
    }
    return  self;
    
}

//  设置UI
- (void)setupUI {
    
    self.fenxiangView = [[UIView alloc]initWithFrame:CGRectMake(20, (kScreenH- 270)/2, kScreenW-40, 320)];
    self.fenxiangView.backgroundColor = RGB(255, 255, 255);
    self.fenxiangView.layer.cornerRadius = 12;
    self.fenxiangView.layer.masksToBounds = YES;
    [self addSubview:self.fenxiangView];
     

    
    UIView  *hunseView = [[UIView alloc]initWithFrame:CGRectMake( 0, 0, kScreenW-40, 102)];
    hunseView.backgroundColor =  [UIColor  bm_colorGradientChangeWithSize:CGSizeMake(kScreenW-40, 160) direction:IHGradientChangeDirectionVertical startColor:RGB(73, 74, 86) endColor:RGB(32, 32, 42)];
    hunseView.hidden = NO;
    [ self.fenxiangView addSubview:hunseView ];
    
    
    self.icoiamgeView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 54, 54)];
    self.icoiamgeView.backgroundColor = [UIColor redColor];
    self.icoiamgeView.layer.cornerRadius = 10;
    self.icoiamgeView.layer.masksToBounds = YES;
    [hunseView addSubview:self.icoiamgeView];
    
    
    UILabel *yonghulabel = [[UILabel alloc]initWithFrame:CGRectMake(self.icoiamgeView.right+10, 18, kScreenW-70-40, 21)];
            //     fenxianlabel3.text = @"1. 每日签到+5积分，连续签到积分翻倍，单日积分+30封顶.\n2. VIP会员用户每月可享5次补签机会，补签可获得补签积分，但无法累计连续签到次数。\n3. 连续签到2周后，签到所得代金券自动发放到我的代金券账户里。 \n4.本次活动最终解释权归九四玩所有";
                yonghulabel.font = [UIFont systemFontOfSize:12];
                yonghulabel.textColor = RGB(255, 255, 255);
                yonghulabel.numberOfLines = 0;
        yonghulabel.text = @"用户名称";
                yonghulabel.textAlignment =  NSTextAlignmentLeft;
              yonghulabel.backgroundColor = [UIColor clearColor];
    self.nichengLabel = yonghulabel;
    [hunseView addSubview:yonghulabel];
    
    
    
    UILabel *shenyuyonghulabel = [[UILabel alloc]initWithFrame:CGRectMake(self.icoiamgeView.right+10, yonghulabel.bottom+3, kScreenW-70-40, 17)];
            //     fenxianlabel3.text = @"1. 每日签到+5积分，连续签到积分翻倍，单日积分+30封顶.\n2. VIP会员用户每月可享5次补签机会，补签可获得补签积分，但无法累计连续签到次数。\n3. 连续签到2周后，签到所得代金券自动发放到我的代金券账户里。 \n4.本次活动最终解释权归九四玩所有";
                shenyuyonghulabel.font = [UIFont systemFontOfSize:12];
                shenyuyonghulabel.textColor = RGB(255, 255, 255);
                shenyuyonghulabel.numberOfLines = 0;
        shenyuyonghulabel.text = @"剩余天数：100 天";
                shenyuyonghulabel.textAlignment =  NSTextAlignmentLeft;
              shenyuyonghulabel.backgroundColor = [UIColor clearColor];
    self.tianshulabel = shenyuyonghulabel;
    
    [hunseView addSubview:shenyuyonghulabel];
    
    
    
    UILabel *buqiancishibel = [[UILabel alloc]initWithFrame:CGRectMake(self.icoiamgeView.right+10, shenyuyonghulabel.bottom+2, kScreenW-70-40, 17)];
            //     fenxianlabel3.text = @"1. 每日签到+5积分，连续签到积分翻倍，单日积分+30封顶.\n2. VIP会员用户每月可享5次补签机会，补签可获得补签积分，但无法累计连续签到次数。\n3. 连续签到2周后，签到所得代金券自动发放到我的代金券账户里。 \n4.本次活动最终解释权归九四玩所有";
    buqiancishibel.font = [UIFont systemFontOfSize:12];
    buqiancishibel.textColor = RGB(255, 255, 255);
    buqiancishibel.numberOfLines = 0;
    buqiancishibel.text = @"当月剩余补签次数：3 次";
    buqiancishibel.textAlignment =  NSTextAlignmentLeft;
    buqiancishibel.backgroundColor = [UIColor clearColor];
    self.buqiansihuLabel = buqiancishibel;
    [hunseView addSubview:buqiancishibel];
    
    
    
    
    
    
    
    
    
    UILabel *fenxianlabel3 = [[UILabel alloc]initWithFrame:CGRectMake(35, 102+20, kScreenW-70-40, 78)];
         //     fenxianlabel3.text = @"1. 每日签到+5积分，连续签到积分翻倍，单日积分+30封顶.\n2. VIP会员用户每月可享5次补签机会，补签可获得补签积分，但无法累计连续签到次数。\n3. 连续签到2周后，签到所得代金券自动发放到我的代金券账户里。 \n4.本次活动最终解释权归九四玩所有";
             fenxianlabel3.font = [UIFont systemFontOfSize:12];
             fenxianlabel3.textColor = RGB(136, 136, 136);
             fenxianlabel3.numberOfLines = 0;
             fenxianlabel3.textAlignment =  NSTextAlignmentLeft;
           fenxianlabel3.backgroundColor = [UIColor whiteColor];
    
     NSString *str = @"尊敬的VIP会员用户，您即将对8.16日进行补签操作，是否要进行补签？";
    
    self.riqimiaoshulabel = fenxianlabel3;
    
    
      [self setLineSpace:8.0f withText:str inLabel:fenxianlabel3];
    [self.fenxiangView  addSubview:fenxianlabel3];
    
    
    
    NSString *lijikai =   [HWjianzhiduiObject keylocalization:@"立即开通"];
   
       UIButton *loginbutt  = [UIButton setButtonWithFrame:CGRectMake(20,fenxianlabel3.bottom+20,kScreenW-80, 44) Title:lijikai Image:nil Font:15 Color:RGB(255, 255, 255) Target:self Selector:@selector(logtapTap:)];
    self.xiaohaoBut =loginbutt;
      //  loginbutt.backgroundColor = RGB(68, 94, 233);
      //  [loginbutt setBackgroundImage:[UIImage imageNamed:@"button_s"] forState:UIControlStateNormal];
    //    loginbutt.layer.shadowColor = [UIColor colorWithRed:26/255.0 green:113/255.0 blue:255/255.0 alpha:0.35].CGColor;
    //    loginbutt.layer.shadowOffset = CGSizeMake(0,0);
    //    loginbutt.layer.shadowOpacity = 1;
    //    loginbutt.layer.shadowRadius = 10;
    //    loginbutt.layer.cornerRadius = 6;
    //    loginbutt.layer.masksToBounds = YES;
    //    loginbutt.clipsToBounds = NO;
    
        loginbutt.backgroundColor = RGB(246, 106, 5);
        loginbutt.layer.masksToBounds = YES;
        loginbutt.layer.cornerRadius = 22;
    loginbutt.backgroundColor = [UIColor bm_colorGradientChangeWithSizeView:loginbutt];
    
        [self.fenxiangView addSubview:loginbutt];
    
    

    
    NSString *quxiao = [HWjianzhiduiObject keylocalization:@"取消"];
    
      UIButton *quxiaologinbutt  = [UIButton setButtonWithFrame:CGRectMake(20,loginbutt.bottom+5,kScreenW-80, 44) Title:quxiao Image:nil Font:15 Color:RGB(136, 136, 136) Target:self Selector:@selector(quexiao:)];
      //  loginbutt.backgroundColor = RGB(68, 94, 233);
      //  [loginbutt setBackgroundImage:[UIImage imageNamed:@"button_s"] forState:UIControlStateNormal];
    //    loginbutt.layer.shadowColor = [UIColor colorWithRed:26/255.0 green:113/255.0 blue:255/255.0 alpha:0.35].CGColor;
    //    loginbutt.layer.shadowOffset = CGSizeMake(0,0);
    //    loginbutt.layer.shadowOpacity = 1;
    //    loginbutt.layer.shadowRadius = 10;
    //    loginbutt.layer.cornerRadius = 6;
    //    loginbutt.layer.masksToBounds = YES;
    //    loginbutt.clipsToBounds = NO;

        quxiaologinbutt.backgroundColor = RGB(255, 255, 255);
        quxiaologinbutt.layer.masksToBounds = YES;
        quxiaologinbutt.layer.cornerRadius = 22;
        [self.fenxiangView addSubview:quxiaologinbutt];



    
    
    

    
    
    
    

//    self.listArr = @[@"微信",@"朋友圈",@"QQ",@"QQ空间"];
//    self.icoArr = @[@"share_w",@"share_q",@"share_qqico",@"share_c"];
//    CGFloat jianju = (kScreenW - 82-4*50)/3;
//    // CGFloat  shujianju = (166-100)/3;
//
//    for (int i = 0 ; i < self.listArr.count; i++) {
//
//
//        UIButton *butt  = [UIButton setButtonWithFrame:CGRectMake(jianju*i+41+i*50, 52, 50, 50) Title:self.listArr[i] Image:self.icoArr[i] Font:13 Color:RGB(51, 51, 51) Target:self Selector:@selector(buttonTap:)];
//        butt.tag = i;
//        [butt layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:10];
//        // butt.backgroundColor = [UIColor redColor];
//        [self.fenxiangView addSubview:butt];
//
//    }
    
//    UIView *fengeview = [[UIView alloc]initWithFrame:CGRectMake(0,137, kScreenW, 1)];
//    fengeview.backgroundColor = RGB(215, 215, 215);
//
//
//    [self.fenxiangView addSubview:fengeview];
//
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//    button.frame = CGRectMake(0, 138, kScreenW, 38);
//    [button setTitle:@"保存" forState:UIControlStateNormal];
//    [button setTitleColor:RGB(51, 51, 51) forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(quiabuttontap) forControlEvents:UIControlEventTouchUpInside];
//    button.titleLabel.font = [UIFont systemFontOfSize:14];
//    [self.fenxiangView addSubview:button];
//
    
    
    
    
    
    
    
    
    
}


-(void)setDataDic:(NSDictionary *)dataDic {
    

   //  [self.xiaohaoBut setTitle:[NSString stringWithFormat:@"%@",jifenStr] forState:UIControlStateNormal];
    
    
    
}


- (void)relayoutTheChildControls {
//    // self.whiteView.backgroundColor = [UIColor redColor];
//    self.whiteView.frame = CGRectMake(0, self.whibackvieY, kScreenW, 120);
//    self.grayView.frame = CGRectMake(0, self.whibackvieY+120, kScreenW, kScreenH-self.whibackvieY-118+10);
//

    
}






// 添加灰色View
-(void)addgrayView {
    
    self.grayView = [[UIView alloc]init];
    self.grayView.frame  = CGRectMake(0,0, kScreenW, kScreenH);
    
    self.grayView.backgroundColor =  RGB(51, 51, 51);
    self.grayView.alpha = 0.9;
    [self addSubview:self.grayView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapView:)];
    //  tap.delegate = self;
    [self.grayView addGestureRecognizer:tap];
    
    
    
    
}

- (void)tapView:(UITapGestureRecognizer *)touch {
    
    [touch.view.superview removeFromSuperview];
    //   [[NSNotificationCenter defaultCenter] postNotificationName:@"notifyName1" object:nil];
    
    
}


//点击buton 按钮

- (void)buttonTap:(UIButton *)sender {
    
 //   [self.delegate xuazhongTag:(int)sender.tag];
    
     
        
    
   
    
}

#pragma mark ------ 取消
- (void)quiabuttontap {
    
  //  [self removeFromSuperview];
    
    
   UIImageWriteToSavedPhotosAlbum(self.fenxiangImge.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    #pragma mark -- <保存到相册>
   
    
    
}

-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    NSString *msg = nil ;
    if(error){
        msg = @"保存图片失败" ;
        [MBProgressHUD showSuccess:@"保存失败"];
        
    }else{
        msg = @"保存图片成功" ;
           [MBProgressHUD showSuccess:@"保存成功"];
    }
}



//-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
//
//    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
//
//        return NO;
//    }else if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableView"]) {
//
//
//        return NO;
//    }
//    return YES;
//
//
//}


//- (void)setUrlStr:(NSString *)urlStr {
//
//     _urlStr = urlStr;
//
//    self.fenxiangImge.image =[self createNonInterpolatedUIImageFormCIImage:[self creatQRcodeWithUrlstring:urlStr] withSize:150];
//
//
//
//
//
//}

- (CIImage *)creatQRcodeWithUrlstring:(NSString *)urlString{
    
    // 1.实例化二维码滤镜
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    // 2.恢复滤镜的默认属性 (因为滤镜有可能保存上一次的属性)
    [filter setDefaults];
    // 3.将字符串转换成NSdata
    NSData *data  = [urlString dataUsingEncoding:NSUTF8StringEncoding];
    // 4.通过KVO设置滤镜, 传入data, 将来滤镜就知道要通过传入的数据生成二维码
    [filter setValue:data forKey:@"inputMessage"];
    // 5.生成二维码
    CIImage *outputImage = [filter outputImage];
    return outputImage;
}
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
{
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}




- (void)logtapTap:(UIButton *)sender  {
    
    
    
//    HWRealNameAuthenticationController *authentication = [[HWRealNameAuthenticationController alloc]init];
//    [[HWViewUtils getCurrentVC].navigationController pushViewController:authentication animated:YES];
  //   [self.delegate  dianjiqiandao];
    
//    NSString *urlStr = [NSString stringWithFormat:@"%@%@",[HWGlobalConst baseUrl],@"api/app/v3/?ct=app&ac=huiyuan&app=1&token=cC0PWoJSpHkFEPvAR2Lh_G2h9TX8lEuyHsKnCKwPDHLyBaXAGuEOB3EuE1ALhA9i&t=1610601403&sign=8c0590448d118badcbb5a6fed9a5b04a"];
//
//    NSString *userID = [HWLocallyStoredData getLoginUserID];
//               NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
//               NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
//               NSString *signMD5 = [WMUtil md5HexDigest:sign];
//
//
//   //  [HWGlobalConst baseUrl]
//        NSString *urlStr = [NSString stringWithFormat:@"%@api/app/v3/?ct=app&ac=huiyuan&app=1&token=%@&t=%@&sign=%@",[HWGlobalConst baseUrl],userID,tStr,signMD5];
//
//
//    XMGWebViewController *webVc = [[XMGWebViewController alloc] init];
//  //   webVc.url = [NSURL URLWithString:urlStr];
//
//    webVc.url = [NSURL URLWithString:urlStr];
//    [[HWViewUtils getCurrentVC].navigationController pushViewController:webVc animated:YES];
            
     [self.delgate zhishibuqian:self.model];
    
     [self removeFromSuperview];
    
    
        
     
    
}

- (void)quexiao:(UIButton *)sender  {
    
      [self removeFromSuperview];
    
}


-(void)setLineSpace:(CGFloat)lineSpace withText:(NSString *)text inLabel:(UILabel *)label{
    if (!text || !label) {
        return;
    }
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpace;  //设置行间距
    paragraphStyle.lineBreakMode = label.lineBreakMode;
    paragraphStyle.alignment = label.textAlignment;
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])];
    label.attributedText = attributedString;
    
    
    
}

// 查询最新的昵称
- (void)zuixingqingqiushuju {
    
          NSString *userID = [HWLocallyStoredData getLoginUserID];
          NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
          NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
          NSString *signMD5 = [WMUtil md5HexDigest:sign];
          NSDictionary *parametes = @{
                                     @"ct" :  @"app",
                                     @"ac" : @"account",
                                     @"ios" : @"1",
                                     @"uid" :userID,
                                     @"t" :tStr,
                                     @"member_id":[HWLocallyStoredData getCode],
                                     @"sign": signMD5
                                     };
    
    
         
     //    [MBProgressHUD show:@"正在绑定邮箱"];
         
         [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
             
           
             NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
          //   [MBProgressHUD hideHUD];
             
             
             if ([codeStr intValue] == 1) {
                 
                 
                 NSString   *faseStr = [NSString stringWithFormat:@"%@",response[@"data"][@"user"][@"face"]];
                 [self.icoiamgeView sd_setImageWithURL:[NSURL URLWithString:faseStr] placeholderImage:nil];
                 
                 self.nichengLabel.text = [NSString stringWithFormat:@"%@",response[@"data"][@"user"][@"user_name"]];
                  
                //   self.shujuDic = response[@"data"];
               
                

             } else {
              
                  [MBProgressHUD  showSuccess:response[@"message"]];
                 
             }
             NSLog(@"%@",response);
             
             
         } failure:^(NSString *error) {

             [MBProgressHUD  showError:@"查询失败"];
             
            
         }];
    
    // 17801317358
    
}



- (void)shuaxingshuju {
    // @"剩余天数：100 天"
//    self.tianshulabel.text = [NSString stringWithFormat:@"剩余天数：%@天",self.huiyuaStr];
//    self.buqiansihuLabel.text = [NSString stringWithFormat:@"当月剩余补签次数：%@次",self.cishuStr];
//
    
    
    self.tianshulabel.text = [NSString stringWithFormat:@"%@:%@天",[HWjianzhiduiObject keylocalization:@"剩余天数"],self.huiyuaStr];
    self.buqiansihuLabel.text = [NSString stringWithFormat:@"%@：%@次",[HWjianzhiduiObject keylocalization:@"当月剩余补签次数"],self.cishuStr];
    
    
    
    NSString *timeStr = [NSString stringWithFormat:@"%@",self.model.time];
    
    
    
     //  @"尊敬的VIP会员用户，您即将对8.16日进行补签操作，是否要进行补签？";
   //  NSString *str =  [NSString stringWithFormat:@"尊敬的VIP会员用户，您即将对%@日进行补签操作，是否要进行补签？",self.model.time];
    
    
    NSString *str =  [NSString stringWithFormat:@"%@%@%@",[HWjianzhiduiObject keylocalization:@"尊敬的VIP会员用户，您即将对"],self.model.time,[HWjianzhiduiObject keylocalization:@"日进行补签操作，是否要进行补签？"]];
    
    self.riqimiaoshulabel.text = str;
    
    
     // self.riqimiaoshulabel.attributedText =    [WMUtil attributedStringWithText:str withRangText:self.model.time withColor:RGB(247, 113, 19) withFont: [UIFont systemFontOfSize:16]];
    
    
//     str1 =   [WMUtil attributedStringWithText:str withColor:RGB(136, 136, 136) withFont:[UIFont systemFontOfSize:12] lineSpacing:8 ziJianJu:1];
    
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
      paragraphStyle.lineSpacing = 8;  //设置行间距
      paragraphStyle.lineBreakMode = self.riqimiaoshulabel.lineBreakMode;
      paragraphStyle.alignment = self.riqimiaoshulabel.textAlignment;
      
      NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
      [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [str length])];
    
    
    [attributedString addAttribute:NSForegroundColorAttributeName value:RGB(248, 109, 25) range:NSMakeRange(15,  10)];
    
     [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(15,  10)];
    // 这个屏蔽了
  //   self.riqimiaoshulabel.attributedText = attributedString;
    
    
   //  [self setLineSpace:8.0f withText:str inLabel:self.riqimiaoshulabel];
    
}


@end
